﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace PowerToolsWeb.Facade
{
    /// <summary>
    /// My97DatePicker 4.6 Release 发布于2009-08-31
    /// </summary>
    public class WdatePicker4_6
    {
        public enum PickerShowType
        {
            Date = 1,
            Time = 2,
            DateTime = 3,
        }

        public WdatePicker4_6(Page page)
        {
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "WdatePickerJs", string.Format(@"<script language=""javascript"" src=""http://{0}js/WdatePicker/WdatePicker.js"" type=""text/javascript""></script>", page.Request.Url.Authority + page.ResolveUrl("~")));
        }

        public WdatePicker4_6(Page page, string SiteRoot)
        {
            page.ClientScript.RegisterClientScriptBlock(page.GetType(), "WdatePickerJs", string.Format(@"<script language=""javascript"" src=""{0}/js/WdatePicker/WdatePicker.js"" type=""text/javascript""></script>", SiteRoot));
        }

        private DateTime? _MinDate;
        /// <summary>
        /// 最小日期
        /// </summary>
        public DateTime? MinDate
        {
            get { return _MinDate; }
            set { _MinDate = value; }
        }

        private DateTime? _MaxDate;
        /// <summary>
        /// 最大日期
        /// </summary>
        public DateTime? MaxDate
        {
            get { return _MaxDate; }
            set { _MaxDate = value; }
        }

        private PickerShowType showType = PickerShowType.Date;
        /// <summary>
        /// 显示类型
        /// </summary>
        public PickerShowType ShowType
        {
            get { return showType; }
            set { showType = value; }
        }

        private string GetShowType()
        {
            string sShowType;
            switch (showType)
            {
                case PickerShowType.Date:
                    sShowType = "yyyy-MM-dd";
                    break;
                case PickerShowType.Time:
                    sShowType = "HH:mm:ss";
                    break;
                case PickerShowType.DateTime:
                    sShowType = "yyyy-MM-dd HH:mm:ss";
                    break;
                default:
                    sShowType = "yyyy-MM-dd";
                    break;
            }
            return sShowType;
        }

        public void Set(TextBox textBox1, TextBox textBox2)//一般为合同时间，开始与结束时间
        {
            string paramText1 = string.Empty;
            paramText1 = string.Format("dateFmt:'{0}'", GetShowType());
            paramText1 += ",maxDate:'#F{$dp.$D(\\'"+textBox2.ClientID+"\\')}'";
            if (_MinDate != null)
            {
                paramText1 += string.Format(",minDate:'{0}'", _MinDate.ToString());
            }
            textBox1.Attributes.Add("onfocus", "WdatePicker({"+paramText1+"});");
            textBox1.Attributes.Add("class", "Wdate");

            string paramText2 = string.Empty;
            paramText2 = string.Format("dateFmt:'{0}'", GetShowType());
            paramText2 += ",minDate:'#F{$dp.$D(\\'"+textBox1.ClientID+"\\')}'";
            if (_MaxDate != null)
            {
                paramText2 += string.Format(",maxDate:'{0}'", _MaxDate.ToString());
            }
            textBox2.Attributes.Add("onfocus", "WdatePicker({"+paramText2+"});");
            textBox2.Attributes.Add("class", "Wdate");
        }

        /// <summary>
        /// 单个文本框设置时间
        /// </summary>
        /// <param name="textBox">要设置的文本框</param>
        public void Set(TextBox textBox)
        {
            string paramText = string.Empty;
            paramText = string.Format("dateFmt:'{0}'", GetShowType());
            if (_MinDate != null)
            {
                paramText += string.Format(",minDate:'{0}'", _MinDate.ToString());
            }
            if (_MaxDate != null)
            {
                paramText += string.Format(",maxDate:'{0}'", _MaxDate.ToString());
            }
            textBox.Attributes.Add("onfocus", "WdatePicker({"+paramText+"});");
            textBox.Attributes.Add("class", "Wdate");
        }
    }
}
